package com.wxzhou.wangzhuan.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.constraints.NotNull;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;

/**
 * 用户收益分页请求查询对象
 * 用于接收客户端传递的时间范围参数，支持预设时间段和自定义时间段查询
 */
public class UserIncomeDashboardRequestDto extends PageRequestDto {

    private Long userid;

    //IncomeSense，场景
    private Integer incomesense;

    //时间枚举
    public enum RangeType {
        TODAY, YESTERDAY, THIS_WEEK,
        THIS_MONTH, THIS_YEAR, CUSTOM
    }

    @NotNull
    private RangeType rangeType;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime customStartDate;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime customEndDate;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime startDateTime;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime endDateTime;

    /**
     * 计算时间范围
     * 根据设置的rangeType自动计算开始和结束时间
     */
    public UserIncomeDashboardRequestDto calculateRange() {
        if (rangeType == null) {
            // 默认查询当天
            rangeType = RangeType.TODAY;
        }
        
        LocalDateTime today = LocalDateTime.now();
        switch (rangeType) {
            case TODAY:
                this.startDateTime = today.withHour(0).withMinute(0).withSecond(0).withNano(0);
                this.endDateTime = today.withHour(23).withMinute(59).withSecond(59).withNano(999_999_999);
                break;
            case YESTERDAY:
                this.startDateTime = today.minusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
                this.endDateTime = today.minusDays(1).withHour(23).withMinute(59).withSecond(59).withNano(999_999_999);
                break;
            case THIS_WEEK:
                this.startDateTime = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY))
                        .withHour(0).withMinute(0).withSecond(0).withNano(0);
                this.endDateTime = today.withHour(23).withMinute(59).withSecond(59).withNano(999_999_999);
                break;
            case THIS_MONTH:
                this.startDateTime = today.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
                this.endDateTime = today.withHour(23).withMinute(59).withSecond(59).withNano(999_999_999);
                break;
            case THIS_YEAR:
                this.startDateTime = today.withDayOfYear(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
                this.endDateTime = today.withHour(23).withMinute(59).withSecond(59).withNano(999_999_999);
                break;
            case CUSTOM:
                if (customStartDate != null) {
                    this.startDateTime = customStartDate.withHour(0).withMinute(0).withSecond(0).withNano(0);
                } else {
                    // 自定义开始时间为空时，默认为当天开始
                    this.startDateTime = today.withHour(0).withMinute(0).withSecond(0).withNano(0);
                }
                
                if (customEndDate != null) {
                    this.endDateTime = customEndDate.withHour(23).withMinute(59).withSecond(59).withNano(999_999_999);
                } else {
                    // 自定义结束时间为空时，默认为当天结束
                    this.endDateTime = today.withHour(23).withMinute(59).withSecond(59).withNano(999_999_999);
                }
                break;
        }
        return this;
    }

    /**
     * 验证日期范围的有效性
     * @return 日期范围是否有效
     */
    public boolean isValidDateRange() {
        if (startDateTime == null || endDateTime == null) {
            return false;
        }
        return !startDateTime.isAfter(endDateTime);
    }

    public UserIncomeDashboardRequestDto() {
    }

    public Long getUserid() {
        return userid;
    }

    public void setUserid(Long userid) {
        this.userid = userid;
    }

    public RangeType getRangeType() {
        return rangeType;
    }

    public void setRangeType(RangeType rangeType) {
        this.rangeType = rangeType;
    }

    public LocalDateTime getCustomStartDate() {
        return customStartDate;
    }

    public void setCustomStartDate(LocalDateTime customStartDate) {
        this.customStartDate = customStartDate;
    }

    public LocalDateTime getCustomEndDate() {
        return customEndDate;
    }

    public void setCustomEndDate(LocalDateTime customEndDate) {
        this.customEndDate = customEndDate;
    }

    public LocalDateTime getStartDateTime() {
        return startDateTime;
    }

    public void setStartDateTime(LocalDateTime startDateTime) {
        this.startDateTime = startDateTime;
    }

    public LocalDateTime getEndDateTime() {
        return endDateTime;
    }

    public void setEndDateTime(LocalDateTime endDateTime) {
        this.endDateTime = endDateTime;
    }

    public Integer getIncomesense() {
        return incomesense;
    }

    public void setIncomesense(Integer incomesense) {
        this.incomesense = incomesense;
    }
}
